package com.chang.controller;

import com.chang.bean.Order;
import com.chang.common.bean.PageBean;
import com.chang.common.bean.ResultBean;
import com.chang.common.crud.BaseService;
import com.chang.common.controller.BaseController;
import com.chang.common.group.ISave;
import com.chang.common.group.V;
import com.chang.query.OrderQuery;
import com.chang.service.impl.OrderSericeImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: erp-api
 * @description:
 * @author: chang
 * @create: 2025-02-14 20h
 **/

/**
 * 控制类
 */
@RestController
@RequestMapping("/api")
@Api(tags = "订单管理")//标签
public class OrderController extends BaseController {

    @Autowired
    OrderSericeImpl orderSerice;

    @ApiOperation("查询订单")
    @GetMapping("/order")
    public ResultBean get(@RequestParam("id") int id) {
        System.out.println(id);
        Order order = orderSerice.get(id);
        return success("客户信息", order);
    }


    @ApiOperation("查询订单列表")
    @PostMapping("/order/list")
    public ResultBean<PageBean<List<Order>>> query(@RequestBody OrderQuery query) {
//        PageBean pageBean=customerService.query(query);
        PageBean bean = orderSerice.queryByMap(BeanUtil.beanToMap(query));
        return success("订单列表", bean);
    }


    @ApiOperation("保存订单")
    @PostMapping("/order")
    public ResultBean save(@Validated(ISave.class) @RequestBody Order order, BindingResult result) {
        V.validate(result);
        int rs = orderSerice.save(order);
        return saveResult(rs);
    }


    @ApiImplicitParam(name = "id", value = "订单id", dataTypeClass = Integer.class, paramType = "query", required = true)
    @ApiOperation("删除订单")
    @DeleteMapping("/order")
    public ResultBean delete(@RequestParam(required = true) int id) {
        int rs = orderSerice.delete(id);
        return deleteResult(rs);
    }


    @ApiOperation("修改订单")
    @PutMapping("/order")
    public ResultBean update(@RequestBody Order order) {
        int rs = orderSerice.update(order);
        return updateResult(rs);
    }

}





